package com.nyc.boot;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author niuyicong
 * @date 2025/8/26  22:55:00
 * @description
 */
@ConditionalOnClass(Redisson.class) // 条件注解
@EnableConfigurationProperties(RedissonProperties.class)
@Configuration
public class RedissonAutoConfiguration {

    /**
     * RedissonClient:操作Redis的核心对象
     *     将该对象注入到容器中
     *     需要连接Redis 服务， host  port
     * @return
     */
    @Bean
    public RedissonClient redissonClient(RedissonProperties redissonProperties){
        Config config = new Config();
        String prefix = "redis://";
        if(redissonProperties.isSsl()){
            prefix = "rediss://";
        }
        config.useSingleServer()
                .setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort())
                .setTimeout(redissonProperties.getTimeout())
                .setPassword(redissonProperties.getPassword());
        return Redisson.create(config);
    }


}
